package com.liang.controller;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/18
 * Time: 23:50
 * Description:
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 发送 延迟消息
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //开始发消息 生产者
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable("message") String message){

        log.info("当前时间:{},发送一条信息给两个ttl 队列:{}",new Date().toString(),message);

        rabbitTemplate.convertAndSend("X","XA","消息来自TTL为10的队列: "+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自TTL为40的队列: "+message);


    }

    @GetMapping("/sendTTLMsg/{message}/{ttl}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttl){

        log.info("当前时间:{},发送一条时长{}毫秒 TTL信息给队列QC:{}",new Date().toString(),ttl,message);

        //发送一条消息 并设置ttl 过期时间
        rabbitTemplate.convertAndSend("X","XC",message,msg->{msg.getMessageProperties().setExpiration(ttl);return msg;});

    }

    @GetMapping("/sendDelayedMsg/{message}/{delayedTime}")
    public void sendDelayedMsg(@PathVariable String message,@PathVariable int delayedTime){

        log.info("当前时间:{},发送信息给延迟队列delayed.queue:{},延迟时间{}ms",new Date().toString(),message,delayedTime);

        //发送一条消息 并设置ttl 过期时间
        rabbitTemplate.convertAndSend("delayed_exchange","delayed.routingKey",message,msg->{msg.getMessageProperties().setDelay(delayedTime);return msg;});

    }




}